package com.example.kotlinview

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.LinearLayout
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.kotlinview.adapter.KtClassBean
import com.example.kotlinview.adapter.KtRvAdapter
import com.example.kotlinview.coroutines.CoroutineAsyncActivity
import com.example.kotlinview.tab.TabActivity
import kotlinx.coroutines.*


class KotlinActivity : AppCompatActivity() {

    var classLists:MutableList<KtClassBean>?= mutableListOf();
    /*
     * 添加列表数据集
     */
    fun getBeans(): MutableList<KtClassBean> {
//let方式
//        classLists?.let {
//            it.add(KtClassBean("okhttp",ktOkhttpActivity::class.java))
//            it.add(KtClassBean("Room",RoomActivity::class.java))
//        }

/*        classLists?.run {
            this.add(KtClassBean("okhttp",ktOkhttpActivity::class.java))
            this.add(KtClassBean("Room",RoomActivity::class.java))
        }*/

//with方式
        var classLists2 = with(mutableListOf<KtClassBean>()) {
            add(KtClassBean("CoroutineAsync",CoroutineAsyncActivity::class.java))
            add(KtClassBean("TabActivity",TabActivity::class.java))
            add(KtClassBean("okhttp", ktOkhttpActivity::class.java))
            add(KtClassBean("Room", RoomActivity::class.java))
            this
        }
        return classLists2;
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_kotlin)
        var rv:RecyclerView=findViewById(R.id.main_kt_rv);
        //设置适配器
        val ktRvAdapter = classLists?.let { KtRvAdapter(it) }
        rv.layoutManager=LinearLayoutManager(this);
        rv.adapter= ktRvAdapter;
        //        rl.setDivider(getContext().getResources().getDrawable(R.id.xxx));
        rv.addItemDecoration(DividerItemDecoration(this@KotlinActivity, LinearLayout.VERTICAL))

        classLists?.let {
            it.clear()
            it.addAll(getBeans())
        }
        //刷新数据
        ktRvAdapter?.notifyDataSetChanged()
        //点击事件
        ktRvAdapter?.setOnItemClickListener(object: KtRvAdapter.OnItemClickListener{
            override fun onItemClick(pos: Int) {
//              这里rv.context 传的是context上下文对象，按照java习惯传this，参数类型不对，是会报错的
                var intent = Intent(rv.context, classLists?.get(pos)?.clazz)
                startActivity(intent)
            }
        })
    }

}
